
#HLA EPITOPES FREQUENCY DATABASE
#Function: Insert data into [epit_frequency] table [EpitHLA] schema
#Created in: 29.01.2014
#Author: Louise Takeshita


use strict;
use EpiHash qw(epi_data);

#Process Epitope data into HashMap
my %epitopes = epi_data; # returns epitope hashmap

#Check for arguments in the command line
if(@ARGV != 1){
	die "Invalid number of arguments!\n";
}
chomp(@ARGV);
my $input = $ARGV[0];
my $pop = substr($input,0,-4);

#Create output file
open(OUTPUT,">insert_$pop.sql") || die "The file insert_$pop.sql could not be opened.\n"; #open file and handle unexpected errors

#Read File
open(INPUT,$input) || die "The file " . $input . " could not be opened.\n"; #open file and handle unexpected errors

#Processing
while(my $line = <INPUT>)
	{
	chomp($line);
	$line =~ s/^\s+//g; #remove leading spaces
	$line =~ s/\s+$//; #remove trailing spaces
	my @line = split(/,/,$line);
	my $epit_id = $epitopes{$line[1]}{'pos'};
	my $freq = sprintf("%.2f", $line[3]);
	print OUTPUT "INSERT INTO EpitHLA.epit_frequency VALUES ('X',$epit_id,$freq);\n";
	}

print "Done!";